using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Oops.Xna.Framework.Physics;

namespace Oops.Xna.Framework.Testing.App
{
	public class Example3Screen : ExampleScreen
	{
		// Content.
		private Texture2D planeTexture;
		
		// Drawing helpers.
		private Quadrilateral quad;

		public Example3Screen(Game game) : base(game, "Example3")
		{
			// Drawing helper for the ground.
			this.quad = new Quadrilateral(Vector3.Zero, Vector3.Up, Vector3.Backward, 260.0f, 260.0f, 10, 10);
		}

		#region ExampleScreen Members

		protected override void DrawWorld(Effect effect, EffectParameter world, EffectParameter texture)
		{
			// Draw the ground.
			world.SetValue(Matrix.Identity);
			texture.SetValue(this.planeTexture);
			effect.CommitChanges();
			this.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, this.quad.Vertices, 0, 4, this.quad.Indices, 0, 2);
		}

		protected override void Reset()
		{
			// Start with 0 boxes and 4 spheres.
			this.Reset(0, 4);

			// Seed the randomizer.
			Random random = new Random();

			// Iterate on all box shapes.
			int boxCount = 10;
			Vector3 previousPosition = Vector3.Zero;
			Vector3 previousExtents = Vector3.Zero;
			for (int i = 0; i < boxCount; i++)
			{
				// Create a collision box.
				CollisionBox box = new CollisionBox();
				box.Extents = random.NextVector3(2.0f, 7.0f);
				box.StaticFriction = 0.5f;
				box.DynamicFriction = 0.5f;
				box.Restitution = 0.6f;

				// Create a rigid body for the current box.
				RigidBody rigidBody = new RigidBody();
				
				// Add the box to the rigid body and initialize.
				rigidBody.CollisionGroup.CollisionShapes.Add(box);
				rigidBody.CalculateMassProperties(1.0f);

				// Determine the position and orientation of the collision box.
				Vector3 position = new Vector3(0.0f, previousPosition.Y + previousExtents.Y + box.Extents.Y + 2.0f, 0.0f);
#if QUATERNION_ORIENTATION
				Quaternion orientation = Quaternion.Normalize(Quaternion.CreateFromYawPitchRoll(float.Epsilon * (i * 3.5f), 0.0f, 0.0f));
#else
				Matrix orientation = Matrix.CreateFromQuaternion(Quaternion.Normalize(Quaternion.CreateFromYawPitchRoll(float.Epsilon * (i * 3.5f), 0.0f, 0.0f)));
#endif
				rigidBody.UpdateTransform(ref position, ref orientation);

				// Assign the box to the quad-tree and shape list.
				this.boxes.Add(box);
				this.physics.RigidBodies.Add(rigidBody);

				// Hold onto previous extents.
				previousPosition = rigidBody.Position;
				previousExtents = box.Extents;
			}

			// Create the ground plane.  
			CollisionPlane plane = new CollisionPlane();
			plane.Normal = Vector3.UnitY;

			// Create a collision group and add the plane.
			CollisionGroup collisionGroup = new CollisionGroup();
			collisionGroup.CollisionShapes.Add(plane);

			// Add the plane's collision group to the collision system.
			this.physics.CollisionSystem.CollisionGroups.Add(collisionGroup);
		}

		#endregion

		#region DrawableGameComponent Members

		protected override void LoadContent()
		{
			// Load all the used texture resources.
			this.planeTexture = this.Game.GetContent().Load<Texture2D>(@".\Content\Textures\plane");
			
			// Call inherited method.
			base.LoadContent();
		}

		#endregion
	}
}
